﻿package com.peachcastle.tools 
{
	import com.greensock.TweenLite;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	/**
	 * 播放完成后派发事件
	 * @eventType	flash.events.Event.COMPLETE
	 */
	[Event(name = "complete", type = "flash.events.Event")]	
	
	/**
	 * 表情播放器,不管什么样的动画，都可以自动播放完移除动画，并派发完成事件
	 * @author Kevin
	 */
	public class SmartPlayer extends EventDispatcher
	{
		////////////////////////////////////////////////////////////////////////////////
		//
		private const ALPHA_DURATION:Number = 0.;
		private const MIN_PLAY_DURATION:Number = 3;			// 最少播放两秒钟
		
		////////////////////////////////////////////////////////////////////////////////
		//
		private var _list:Array = null;		
		
		private var _buffer:Object = null;
		
		/**
		 * 智能播放器
		 */
		public function SmartPlayer() 
		{
			_buffer = new Object();
		}
		
		/**
		 * 播放动画
		 * @param	animation
		 * @param	userId		使用用户id进行缓存
		 */
		public function play(animation:MovieClip, userId:int):void
		{	
			// 移除
			if (_buffer[userId] != null)
			{	
				var clip:MovieClip = _buffer[userId];
				hideClip(clip);
				resetClip(clip);
				if (clip["tween"]) clip["tween"].kill();
			}

			_buffer[userId] = animation;

			_list = [];
			resetClip(animation, _list);
			_list.sort(sortRule);
			
			// 生成动画
			var removeAction:Function = getRemoveAction();
			removeAction.prototype["userId"] = userId;
			removeAction.prototype["clip"] = animation;
			
			// 取出最长剪辑
			var longClip:MovieClip = _list[0];
			if (longClip.totalFrames / 31 < MIN_PLAY_DURATION)
			{				
				animation["tween"] = TweenLite.delayedCall(MIN_PLAY_DURATION, removeAction);
			}
			else
			{
				longClip.addFrameScript(longClip.totalFrames - 1, removeAction);
			}
			
			//
			for (var i:int = 0; i < _list.length; i++)
			{
				clip = _list[i];
				clip.play();
			}
		}
		
		/**
		 * 停止播放
		 */
		public function stop():void
		{
			var clip:MovieClip = null;
			for (var i:int = 0; i < _list.length; i++)
			{
				clip = _list[i];
				clip.stop();
			}
		}
		
		/**
		 * 返回帧代码移除函数
		 * @return
		 */
		private function getRemoveAction():Function
		{
			var clip:MovieClip = null;
			var action:Function = function():void
			{		
				clip = action.prototype["clip"];
				clip.stop();
				
				//
				resetClip(clip, null);
				
				// 取消引用
				_buffer[action.prototype["userId"]] = null;
				
				//
				TweenLite.to(clip, ALPHA_DURATION, { alpha:0, onComplete:hideClip, onCompleteParams:[clip] } );
				
				//
				dispatchEvent(new Event(Event.COMPLETE));
			};
			
			//
			return action;
		}
		
		/**
		 * 隐藏剪辑
		 */
		private function hideClip(clip:MovieClip):void
		{				
			clip.parent && clip.parent.removeChild(clip);
		}
		
		/**
		 * 排序
		 * @param	clip1
		 * @param	clip2
		 */
		private function sortRule(clip1:MovieClip,clip2:MovieClip):int
		{
			if (clip1.totalFrames < clip2.totalFrames)
				return 1;
			return -1;
		}		
		
		/**
		 * 初始化剪辑
		 * @param	clip
		 * @param	result
		 */
		private function resetClip(clip:DisplayObjectContainer, result:Array = null):void
		{
			var child:DisplayObject = null;
			for (var i:int = 0; i < clip.numChildren; i++)
			{
				child = clip.getChildAt(i);
				if (child is MovieClip)
				{
					if (result) result.push(child);
					(child as MovieClip).stop();
				}
				
				if (child is DisplayObjectContainer)
					resetClip(child as DisplayObjectContainer, result);	
			}
		}
	}
	
}